
unsigned short
XDNS_GET_16BIT(unsigned char* buffer)
{
    return ((buffer[0] << 8) |
            buffer[1]);
}

long
XDNS_GET_32BIT(unsigned char* buffer)
{
    return ((buffer[0] << 24) |
            (buffer[1] << 16) |
            (buffer[2] << 8) |
            buffer[3]);
}

void
XDNS_SET_16BIT(unsigned char* buffer,
               unsigned short value)
{
    buffer[0] = ((value >> 8) & 0xff);
    buffer[1] = (value & 0xff);
}

void
XDNS_SET_32BIT(unsigned char* buffer,
               long value)
{
    buffer[0] = ((value >> 24) & 0xff);
    buffer[1] = ((value >> 16) & 0xff);
    buffer[2] = ((value >> 8) & 0xff);
    buffer[3] = (value & 0xff);
}

unsigned short
XDNS_GET_HEADER_ID(unsigned char* header)
{
    return XDNS_GET_16BIT(header);
}

unsigned char
XDNS_GET_HEADER_QR(unsigned char* header)
{
    return ((header[2] >> 7) & 0x1);
}

unsigned char
XDNS_GET_HEADER_OPCODE(unsigned char* header)
{
    return ((header[2] >> 3) & 0xf);
}

unsigned char
XDNS_GET_HEADER_AA(unsigned char* header)
{
    return ((header[2] >> 2) & 0x1);
}

unsigned char
XDNS_GET_HEADER_TC(unsigned char* header)
{
    return ((header[2] >> 1) & 0x1);
}

unsigned char
XDNS_GET_HEADER_RD(unsigned char* header)
{
    return (header[2] & 0x1);
}

unsigned char
XDNS_GET_HEADER_RA(unsigned char* header)
{
    return ((header[3] >> 7) & 0x1);
}

unsigned char
XDNS_GET_HEADER_Z(unsigned char* header)
{
    return ((header[3] >> 4) & 0x7);
}

unsigned char
XDNS_GET_HEADER_RCODE(unsigned char* header)
{
    return (header[3] & 0xf);
}

unsigned short
XDNS_GET_HEADER_QDCOUNT(unsigned char* header)
{
    return XDNS_GET_16BIT(header + 4);
}

unsigned short
XDNS_GET_HEADER_ANCOUNT(unsigned char* header)
{
    return XDNS_GET_16BIT(header + 6);
}

unsigned short
XDNS_GET_HEADER_NSCOUNT(unsigned char* header)
{
    return XDNS_GET_16BIT(header + 8);
}

unsigned short
XDNS_GET_HEADER_ARCOUNT(unsigned char* header)
{
    return XDNS_GET_16BIT(header + 10);
}

void
XDNS_SET_HEADER_ID(unsigned char* header,
                   unsigned short value)
{
    XDNS_SET_16BIT(header, value);
}

void
XDNS_SET_HEADER_QR(unsigned char* header,
                   unsigned char value)
{
    header[2] |= ((value & 0x1) << 7);
}

void
XDNS_SET_HEADER_OPCODE(unsigned char* header,
                       unsigned char value)
{
    header[2] |= ((value & 0xf) << 3);
}

void
XDNS_SET_HEADER_AA(unsigned char* header,
                   unsigned char value)
{
    header[2] |= ((value & 0x1) << 2);
}

void
XDNS_SET_HEADER_TC(unsigned char* header,
                   unsigned char value)
{
    header[2] |= ((value & 0x1) << 1);
}

void
XDNS_SET_HEADER_RD(unsigned char* header,
                   unsigned char value)
{
    header[2] |= (value & 0x1);
}

void
XDNS_SET_HEADER_RA(unsigned char* header,
                   unsigned char value)
{
    header[3] |= ((value & 0x1) << 7);
}

void
XDNS_SET_HEADER_Z(unsigned char* header,
                  unsigned char value)
{
    header[3] |= ((value & 0x7) << 4);
}

void
XDNS_SET_HEADER_RCODE(unsigned char* header,
                      unsigned char value)
{
    header[3] |= (value & 0xf);
}

void
XDNS_SET_HEADER_QDCOUNT(unsigned char* header,
                        unsigned short value)
{
    XDNS_SET_16BIT(header + 4, value);
}

void
XDNS_SET_HEADER_ANCOUNT(unsigned char* header,
                        unsigned short value)
{
    XDNS_SET_16BIT(header + 6, value);
}

void
XDNS_SET_HEADER_NSCOUNT(unsigned char* header,
                        unsigned short value)
{
    XDNS_SET_16BIT(header + 8, value);
}

void
XDNS_SET_HEADER_ARCOUNT(unsigned char* header,
                        unsigned short value)
{
    XDNS_SET_16BIT(header + 10, value);
}

void
XDNS_SET_QUESTION_TYPE(unsigned char* qtype,
                       unsigned short value)
{
    XDNS_SET_16BIT(qtype, value);
}

void
XDNS_SET_QUESTION_CLASS(unsigned char* qtype,
                        unsigned short value)
{
    XDNS_SET_16BIT(qtype + 2, value);
}

unsigned short
XDNS_GET_QUESTION_TYPE(unsigned char* qtype)
{
    return XDNS_GET_16BIT(qtype);
}

unsigned short
XDNS_GET_QUESTION_CLASS(unsigned char* qtype)
{
    return XDNS_GET_16BIT(qtype + 2);
}

unsigned short
XDNS_GET_RR_TYPE(unsigned char* type)
{
    return XDNS_GET_16BIT(type);
}

unsigned short
XDNS_GET_RR_CLASS(unsigned char* type)
{
    return XDNS_GET_16BIT(type + 2);
}

long
XDNS_GET_RR_TTL(unsigned char* type)
{
    return XDNS_GET_32BIT(type + 4);
}

unsigned short
XDNS_GET_RR_RDLENGTH(unsigned char* type)
{
    return XDNS_GET_16BIT(type + 8);
}

void
XDNS_SET_RR_TYPE(unsigned char* type,
                 unsigned short value)
{
    XDNS_SET_16BIT(type, value);
}

void
XDNS_SET_RR_CLASS(unsigned char* type,
                  unsigned short value)
{
    XDNS_SET_16BIT(type + 2, value);
}

void
XDNS_SET_RR_TTL(unsigned char* type,
                long value)
{
    XDNS_SET_32BIT(type + 4, value);
}

void
XDNS_SET_RR_RDLENGTH(unsigned char* type,
                     unsigned short value)
{
    XDNS_SET_16BIT(type + 8, value);
}

